<?php
/**
 * @title 3072. 将元素分配到两个数组中 II
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
///
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[]
     */
    function resultArray($nums) {
        /**
         * @since 2024-03-04 nums的元素个数
         * @since 2024-03-04 arr1和arr2初始化
         */
        $numCnt = count($nums);
        list($last1, $last2) = [$nums[0], $nums[1]];
        $arr1 = $sort1 = [$last1];
        $arr2 = $sort2 = [$last2];

        /**
         * @since 2024-03-04 遍历nums剩下元素
         */
        for ($i=2; $i<$numCnt; $i++){
            $num = $nums[$i];

            /**
             * @since 2024-03-04 如果 greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]) ，将 nums[i] 追加到 arr1
             */
            $gCnt1 = $this->GreaterCount($sort1, $num);
            $gCnt2 = $this->GreaterCount($sort2, $num);
            // echo "num={$num}, greater = [{$gCnt1}, {$gCnt2}]", PHP_EOL;
            if($gCnt1 > $gCnt2){
                $arr1[] = $num;
                $sort1[] = $num;
                rsort($sort1);
            } elseif($gCnt1 < $gCnt2){
                $arr2[] = $num;
                $sort2[] = $num;
                rsort($sort2);
            } else {
                /**
                 * @since 2024-03-04 如果 greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]) ，将 nums[i] 追加到元素数量较少的数组中。
                 * 如果仍然相等，那么将 nums[i] 追加到 arr1 。
                 */
                if(count($arr1) <= count($arr2)){
                    $arr1[] = $num;
                    $sort1[] = $num;
                    rsort($sort1);
                } else {
                    $arr2[] = $num;
                    $sort2[] = $num;
                    rsort($sort2);
                }
            }
        }

        /**
         * @since 2024-03-04 通过连接数组 arr1 和 arr2 形成数组 result
         * @return Integer[]
         */
        return array_merge($arr1, $arr2);
    }

    /**
     * @title 返回数组 arr 中 严格大于 val 的元素数量
     * @author start2004
     * @since 2024-03-04 Created
     *
     * @param Array $arr 降序排列
     * @param Integer $val
     * @return Integer
     */
    function GreaterCount(array $arr, int $val){
        /**
         * @since 2024-03-04 二分查找
         */
        $s = 0;
        $e = count($arr)-1;
        while($s<=$e){
            $m = ($s+$e)>>1;
            $v = $arr[$m];

            if($v > $val){
                $s = $m+1;
            } else {
                $e = $m-1;
            }
        }

        /**
         * @return Integer
         */
        return $s;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202403/2024.03.04_5.php
 */
$datas = [
    // [[2,1,3,3]], // 2,3,1,3
    // [[5,14,3,1,2]], // 5,3,1,2,14
    // [[3,3,3,3]],
    [[13,53,82,67]], // 13, 82, 67, 53
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->resultArray(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
